import { administratorClassroom, ADMINISTRATOR } from '../src/lib/Administrator'
import { Classrooms } from '../src/lib/Classrooms'
import { okMsg } from "../src/utils/socket_message";
import {MemberEntity, UserType} from "../src/httpServer/entity/MemberEntity";
import { In } from "typeorm";

/*export async function adminUpdate () {
  const adminClass = administrator.get(ADMINISTRATOR) ?? new Set();
  const [dbUser] = await MemberEntity.findAndCountBy({
    userType: In([UserType.admin, UserType.administrator])
  });

  if (Array.isArray(dbUser)) {
    for (const member of dbUser) {
      adminClass.add(member.userId)
      console.log('------------------- adminUpdate -----------------------')
      console.log('adminUpdate: => ', administrator)
    }

    administrator.set(ADMINISTRATOR, adminClass)
  }

  return adminClass
}*/

/*export async function adminMiddleware (app, io) {
  const { socket } = app.locals;
  const { userId, username, userType } = socket.handshake.query;
  const adminClass = administrator.get(ADMINISTRATOR);
  console.log('------------------- adminMiddleware -----------------------')

  /!**
   * 如果这个人是管理员，则加入管理组。
   *!/
  if (adminClass && adminClass.has(userId)) {
    socket.join(ADMINISTRATOR)
    socket.emit('classroom', Classrooms.toArray())
  }

  /!**
   *所有人登陆时，都通知管理组
   *!/
  socket
    .broadcast
    .to(ADMINISTRATOR)
    .emit('member:online', { userId, username } as any);

  /!**
   * 房间创建成功，通知管理组
   *!/
  if (!socket.broadcastCreatedForAdmin) {
    socket.broadcastCreatedForAdmin = function broadcastCreatedForAdmin(data) {
      console.warn(`created =>`, data)
      socket
        .broadcast
        .to(ADMINISTRATOR)
        .emit('classroom:created', data);

      if (socket.broadcastClassroomForAdmin) {
        socket.broadcastClassroomForAdmin()
      }
    }
  }

  /!**
   * 用户状态变化时，通知管理组
   *!/
  if (!socket.broadcastMemberForAdmin) {
    socket.broadcastMemberForAdmin = function broadcastMemberForAdmin(data) {
      console.warn(`member:state =>`, data)
      socket
        .broadcast
        .to(ADMINISTRATOR)
        .emit('member:state', data);
    }
  }

  /!**
   * 推送所有的房间到管理组
   *!/
  if (!socket.broadcastClassroomForAdmin) {
    socket.broadcastClassroomForAdmin = function broadcastClassroomForAdmin() {
      const data = Classrooms.toArray()
      console.warn(`classroom =>`, data)
      socket
        .broadcast
        .to(ADMINISTRATOR)
        .emit('classroom', data);
    }
  }

  /!**
   * 房间状态变化时，通知管理组
   *    1. 创建
   *    2. 有人加入
   *    3. 解散
   *!/
  if (!socket.broadcastClassForAdmin) {
    socket.broadcastClassForAdmin = function broadcastClassForAdmin(data) {
      console.warn(`classroom:state =>`, data)
      socket
        .broadcast
        .to(ADMINISTRATOR)
        .emit('classroom:state', data);
    }
  }

  /!**
   * 更新管理员组
   *!/
  socket.on('administrator:refresh', async function(cb) {
    const data = await adminUpdate()
    cb && cb(okMsg('administrator:refresh', { members: data?.values() }))
  });
}*/
